home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / fonttools / frompsf.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  150 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    frompsf -
  19.  *        Convert a .psf font into the Hex format.
  20.  *
  21.  *                Paul Haeberli - 1990
  22.  */
  23. #include "stdio.h"
  24. #include "ctype.h"
  25.  
  26. unsigned char *charfiledata();
  27.  
  28. FILE *inf, *outf;
  29. int macfont;
  30.  
  31. sizeoffile(f)
  32. FILE *f;
  33. {
  34.     register int pos, ret;
  35.  
  36.     pos = ftell(f);
  37.     if ((ret = fseek(f,0,2)) < 0) {
  38.         fprintf(stderr,"sizeoffile: seek error\n");
  39.         exit(1);
  40.     }
  41.     ret = ftell(f);
  42.     if (fseek(f,pos,0) < 0) {
  43.         fprintf(stderr,"sizeoffile: seek error\n");
  44.         exit(1);
  45.     }
  46.     return ret;
  47. }
  48.  
  49. unsigned char *charfiledata(f)
  50. FILE *f;
  51. {
  52.     unsigned char *dat;
  53.     int nbytes;
  54.  
  55.     nbytes = sizeoffile(f);
  56.     if (fseek(f,0,0) < 0) {
  57.         fprintf(stderr,"charfiledata: seek error\n");
  58.         exit(1);
  59.     }
  60.     dat = (unsigned char *)malloc(nbytes);
  61.     fread(dat,1,nbytes,f);
  62.     if (fseek(f,0,0) < 0) {
  63.         fprintf(stderr,"charfiledata: seek error\n");
  64.         exit(1);
  65.     }
  66.     return dat;
  67. }
  68.  
  69. main(argc,argv)
  70. int argc;
  71. char **argv;
  72. {
  73.     int d;
  74.     char str[256];
  75.     int i, temp;
  76.     int nbytes, maxpos, patlen;
  77.     unsigned char *filedata;
  78.     int beginpos, endpos, count;
  79.     int col;
  80.  
  81.     if(argc<3) {
  82.     fprintf(stderr,"usage: frompsf in.psf out.hex\n");
  83.     exit(1);
  84.     }
  85.     inf = fopen(argv[1],"r");
  86.     if(!inf) {
  87.     fprintf(stderr,"frompsf: can't open input file\n");
  88.     exit(1);
  89.     }
  90.     outf = fopen(argv[2],"w");
  91.     macfont = 0;
  92.     nbytes = sizeoffile(inf);
  93.  
  94.     filedata = charfiledata(inf);
  95.  
  96.     patlen = strlen("currentfile eexec\r");
  97.     maxpos = nbytes-patlen;
  98.     for(i=0; i<nbytes; i++) {
  99.     if(strncmp(filedata+i,"currentfile eexec\r",patlen) == 0) {
  100.         beginpos = i+patlen;
  101.         break;
  102.     }
  103.     }
  104.     if(i == nbytes) {
  105.     fprintf(stderr,"frompsf: eexec string not found\n");
  106.     exit(1);
  107.     }
  108.     for(i=0; i<beginpos; i++) {
  109.     if(filedata[i] == '\r')
  110.         filedata[i] = '\n';
  111.     }
  112.     fwrite(filedata,1,beginpos,outf);
  113.  
  114.     i = nbytes;
  115.     count = 0;
  116.     while(i--) {
  117.     if(filedata[i] == '0') {
  118.         count++;
  119.         if(count == 512) 
  120.         break;
  121.     } else if(!isspace(filedata[i])) {
  122.         count = 0;
  123.     }
  124.     }
  125.     if(count != 512) {
  126.     fprintf(stderr,"frompsf: can't find 512 zeros!!\n");
  127.     exit(1);
  128.     }
  129.     endpos = i;
  130.     col = 0;
  131.     for(i=beginpos; i<endpos; i++) {
  132.     fprintf(outf,"%02x",filedata[i]);
  133.     col++;
  134.     if(col == 32) {
  135.         col = 0;
  136.         fprintf(outf,"\n");
  137.     }
  138.     }
  139.     if(col != 0) 
  140.     fprintf(outf,"\n");
  141.     for(i=endpos; i<nbytes; i++) {
  142.     if(filedata[i] == '\r')
  143.         filedata[i] = '\n';
  144.     }
  145.     fwrite(filedata+endpos,1,nbytes-endpos,outf);
  146.     fclose(outf);
  147.     exit(0);
  148. }
  149.  
  150.